import { Controller } from "../../mvc/core/Controller";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { SSkyCityOpen } from "../../../Message/Public/SSkyCityOpen";
import { SSkyCityList } from "../../../Message/Public/SSkyCityList";
import { SSkyCityInfo } from "../../../Message/Public/SSkyCityInfo";
import { SkyCityEnterData } from "../view/skyList/SkyCityEnterData";
import { GameConst } from "../../component/gconst/GameConst";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { SEntityId } from "../../../Message/Public/SEntityId";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { SkyCityIconView } from "../view/skyList/SkyCityIconView";
import { SkyCityEnterWindow } from "../view/skyList/SkyCityEnterWindow";
//class SkyCityController
    
    export  class SkyCityController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.SkyCityOpenCommand, this.onSkyCityOpenCommand);
            NetDispatcher.addCmdListener(ServerCommand.SkyCityCloseCommand, this.onSkyCityCloseCommand);
            NetDispatcher.addCmdListener(ServerCommand.SkyCityListCommand, this.onSkyCityListCommand);
            Dispatcher.addEventListener(EventName.SkyCityShowEnterWindow, this.onSkyCityShowEnterWindow);
            Dispatcher.addEventListener(EventName.SkyCityRequestList, this.onSkyCityRequestList);
            Dispatcher.addEventListener(EventName.SkyCityRequestEnter, this.onSkyCityRequestEnter);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            return;
        }

        /* internal  */onSkyCityOpenCommand(arg1: SSkyCityOpen): void
        {
            this._isInSkyCityActive = true;
            this.skyCityIconView.show();
            return;
        }

        /* internal  */onSkyCityCloseCommand(arg1: Object): void
        {
            this._isInSkyCityActive = false;
            if (this._skyCityIconView) 
            {
                this.skyCityIconView.hide();
            }
            if (this._skyCityEnterWindow) 
            {
                this.skyCityEnterWindow.hide();
            }
            return;
        }

        /* internal  */onSkyCityListCommand(arg1: SSkyCityList): void
        {
            var loc3=null;
            var loc4=null;
            var loc1=new Array<any>();
            var loc2=0;
            while (loc2 < arg1.skyCities.length) 
            {
                loc3 = arg1.skyCities[loc2] as SSkyCityInfo;
                (loc4 = new SkyCityEnterData()).index = loc2 + 1;
                loc4.maxPlayerNum = GameConst.SkyCityMaxPlayerNum;
                loc4.playerNum = loc3.playerNum;
                loc4.skyCityId = loc3.skyCityId;
                loc1.push(loc4);
                ++loc2;
            }
            this.skyCityEnterWindow.updateList(loc1);
            return;
        }

        /* internal  */onSkyCityShowEnterWindow(arg1: DataEvent): void
        {
            this.skyCityEnterWindow.show();
            return;
        }

        /* internal  */onSkyCityRequestList(arg1: DataEvent): void
        {
            GameProxy.skyCity.getSkyCityList();
            return;
        }

        /* internal  */onSkyCityRequestEnter(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEntityId;
            GameProxy.skyCity.enterSkyCity(loc1);
            return;
        }

        /* internal  */onSceneUpdate(arg1: DataEvent): void
        {
            if (GameMapUtil.curMapState.isSkyCity) 
            {
                if (!this._isInSkyCity) 
                {
                    this._isInSkyCity = true;
                    if (this._skyCityIconView) 
                    {
                        this.skyCityIconView.hide();
                    }
                    if (this._skyCityEnterWindow) 
                    {
                        this.skyCityEnterWindow.hide();
                    }
                }
            }
            else if (this._isInSkyCity) 
            {
                this._isInSkyCity = false;
                if (this._isInSkyCityActive) 
                {
                    if (this._skyCityIconView) 
                    {
                        this.skyCityIconView.show();
                    }
                }
            }
            return;
        }

        /* internal  */get skyCityIconView(): SkyCityIconView
        {
            if (!this._skyCityIconView) 
            {
                this._skyCityIconView = new SkyCityIconView();
            }
            return this._skyCityIconView;
        }

        /* internal  */get skyCityEnterWindow(): SkyCityEnterWindow
        {
            if (!this._skyCityEnterWindow) 
            {
                this._skyCityEnterWindow = new SkyCityEnterWindow();
            }
            return this._skyCityEnterWindow;
        }

        public get isInSkyCityActive(): Boolean
        {
            return this._isInSkyCityActive;
        }

        private /* var */_isInSkyCityActive: Boolean=false;

        private /* var */_isInSkyCity: Boolean=false;

        private /* var */_skyCityIconView: SkyCityIconView;

        private /* var */_skyCityEnterWindow: SkyCityEnterWindow;
    }
